package com.oss.robot.project;

import java.util.ArrayList;
import java.util.List;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.nodes.TagNode;
import org.htmlparser.tags.Div;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.tags.Span;
import org.htmlparser.tags.TableColumn;
import org.htmlparser.tags.TableTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

import com.oss.model.Constants;
import com.oss.model.Project;
import com.oss.model.Task;
import com.oss.robot.AbstractRetrieveRobot;

/**
 * 归类提取了Java-Source.NET的所有项目，需要后续进行人工/半自动分类 TODO 分类较差，项目总体质量不高，不用也罢。可以考虑二期时使用
 * 
 * @author xueqiang.mi
 * @since 2010-7-13
 */
@Deprecated
public class _JavaSourceRobot extends AbstractRetrieveRobot {
	static public final String URL = "http://java-source.net/";
	static public final String SOURCE = "Java-Source";
	static public final Integer TASK_NUMBER_PER_TIME = 1;

	@Override
	@SuppressWarnings("serial")
	public void extractData(Task task) throws ParserException {
		String link = task.getLink();
		Parser parser = new Parser(link);
		NodeList nodeList = parser.extractAllNodesThatMatch(new NodeFilter() {
			@Override
			public boolean accept(Node node) {
				if (node instanceof Div) {
					Div div = (Div) node;
					String id = div.getAttribute("id");
					if (id != null && id.equals("body")) {
						return true;
					}
				}
				return false;
			}
		});
		nodeList = nodeList.extractAllNodesThatMatch(new NodeFilter() {
			@Override
			public boolean accept(Node node) {
				if (node instanceof Span) {
					return true;
				}
				if (node instanceof TableColumn) {
					return true;
				}
				return false;
			}
		}, true);
		for (int i = 0; i < nodeList.size() / 2; i++) {
			Project prj = new Project();
			prj.setTag("others");
			prj.setSource(SOURCE);
			prj.setEnabled(true);
			Tag tag1 = (Tag) nodeList.elementAt(i * 2);
			String prj_name = getName(tag1);
			if (prj_name == null) {
				continue;
			}
			prj.setName(getName(tag1));
			Tag tag2 = (Tag) nodeList.elementAt(i * 2 + 1);
			prj.setDescription(getDescription(tag2));
			if (tag1.getFirstChild() instanceof LinkTag) {
				LinkTag lnk = (LinkTag) tag1.getFirstChild();
				parser = new Parser(lnk.getLink());
				NodeList nodes = parser
						.extractAllNodesThatMatch(new NodeFilter() {
							@Override
							public boolean accept(Node node) {
								if (node instanceof TableTag) {
									String border = ((Tag) node)
											.getAttribute("border");
									if (border == null) {
										return true;
									}
								}
								return false;
							}
						});
				nodes = nodes.extractAllNodesThatMatch(new NodeFilter() {
					public boolean accept(Node node) {
						if (node instanceof TableColumn) {
							if (((Tag) node).getFirstChild().getText().trim()
									.equals("HomePage")) {
								return true;
							}
						}
						return false;
					}
				}, true);
				if (nodes.size() == 1) {
					Node column = nodes.elementAt(0).getNextSibling();
					while (!(column instanceof TableColumn)) {
						column = column.getNextSibling();
					}
					LinkTag linkTag = (LinkTag) column.getFirstChild();
					prj.setUrl(linkTag.getLink());
				}
				prjService.add(prj);
			}
		}
	}

	private String getDescription(Tag tag2) {
		String desc = "";
		if (tag2.getFirstChild() != null) {
			desc = tag2.getFirstChild().getText().trim()
					.replaceAll("\\s+", " ");
		}
		return desc;
	}

	private String getName(Tag tag1) {
		String[] invalidChars = { "(", ":", ",", " - " };
		String name = "XXXX";
		if (tag1.getFirstChild().getFirstChild() != null) {
			name = tag1.getFirstChild().getFirstChild().getText().trim();
		}
		for (String c : invalidChars) {
			if (name.contains(c)) {
				return name.substring(0, name.indexOf(c)).trim();
			}
		}
		if (name.contains("™")) {
			return null;
		}
		if (name.contains(" / ")) {
			return name.replace(" / ", "-").trim();
		}
		if (name.contains(" & ")) {
			return name.replace(" & ", "-").trim();
		}
		return name;
	}

	@Override
	public String getSource() {
		return SOURCE;
	}

	@Override
	public Integer getTasksNumber() {
		return TASK_NUMBER_PER_TIME;
	}

	@Override
	@SuppressWarnings("serial")
	public List<Task> retrieveLinks() throws Exception {
		Parser parser = new Parser(URL);
		NodeList nodeList = parser.extractAllNodesThatMatch(new NodeFilter() {
			@Override
			public boolean accept(Node node) {
				if (node instanceof TableTag) {
					TagNode tag = (TableTag) node;
					String id = tag.getAttribute("id");
					if (id != null && id.equals("leftmenu")) {
						return true;
					}
					return false;
				}
				return false;
			}
		});
		nodeList = nodeList.extractAllNodesThatMatch(new NodeFilter() {
			@Override
			public boolean accept(Node node) {
				if (node instanceof LinkTag) {
					return true;
				}
				return false;
			}
		}, true);
		List<Task> tasks = new ArrayList<Task>();
		for (Node node : nodeList.toNodeArray()) {
			LinkTag link = (LinkTag) node;
			Task task = new Task(link.getLink(), SOURCE,
					Constants.ENTRY_PROJECT);
			tasks.add(task);
		}
		return tasks;
	}
}
